<?php
/**
 * Created by PhpStorm.
 * User: 共赢岛
 * Date: 2024/1/25
 * Time: 15:03
 */

namespace App\Admin\Services;


use App\Admin\Repositories\Car;
use App\Models\Brand;
use App\Models\CarPackage;
use App\Models\CarPackageCategory;
use App\Models\CarPackageOrder;
use App\Models\CarPackageOrderAttr;
use App\Models\CarPackageUsedLog;
use App\Models\Store;
use App\Models\User;
use App\Models\UserCar;
use App\Services\LifeCycle\OrderService;

class PackageImportService
{
    public static function userCreate($param = [])
    {
        $user = User::where('phone', $param['phone'])->first();

        if ($user) {
            return $user;
        } else {
            $user_id = User::create([
                'phone' => $param['phone'],
                'id_card' => $param['id_card']
            ])->id;

            $user = User::find($user_id);
            $user->created_at = $param['created_at'];
            $user->save();

            return $user;
        }
    }

    public static function orderCreate($param = [])
    {
        $order = CarPackageOrder::where('user_id', $param['user']->id)->first();

        if (!$order) {
            $brand = Brand::where([['title', 'like', "%{$param['brand']}%"]])->first();
            $store = Store::where([['title', 'like', "%{$param['store']}%"]])->first();

            $category = CarPackageCategory::where('brand_id', $brand->id)
                ->where([['title', 'like', "%{$param['package_category']}%"]])
                ->first();

            $package = CarPackage::where('brand_id', $brand->id)
                ->where('category_id', $category->id)
                ->where([['title', 'like', "%{$param['package']}%"]])
                ->first();

            $merge_package_ids = '';

            if ($package->merge_package_type == DEFAULT_TYPE_ONE) {
                $merge_package_ids = $package->merge_package_ids;
            }

            $order_sn = OrderService::sn();

            $order_id = CarPackageOrder::create([
                'brand_id' => $package->brand_id,
                'category_id' => $package->category_id,
                'store_id' => $store->id,
                'order_sn' => $order_sn,
                'payment_sn' => $order_sn,
                'package_id' => $package->id,
                'package_title' => $package->title,
                'package_pic' => $package->pic,
                'merge_package_ids' => $merge_package_ids,
                'user_id' => $param['user']->id,
                'user_name' => $param['user']->name,
                'user_phone' => $param['user']->phone,
                'price' => $package->price,
                'order_price' => $param['price'],
                'status' => ORDER_STATUS_PAID,
                'payment_at' => $param['created_at']
            ])->id;

            $order = CarPackageOrder::find($order_id);

            $order->created_at = $param['created_at'];
            $order->code_url = OrderService::orderCodeCreate($order->id);
            $order->code_number = OrderService::orderCodeNumber($order->id);
            $order->created_at = $param['created_at'];

            $order->save();

            OrderService::orderAttrCreate($order);

            UserCar::create([
                'user_id' => $param['user']->id,
                'order_id' => $order_id,
                'brand_id' => $brand->id,
                'car_engine' => $param['car_engine'],
                'car_frame' => $param['car_frame'],
                'car_number' => $param['car_number'] ?? ''
            ]);
        }
    }

    public static function attrUsed($param = [])
    {
        $user = User::where('phone', $param['phone'])->first();

        if ($user) {
            $order = CarPackageOrder::where('user_id', $user->id)->first();

            if ($order) {
                $package_attr = CarPackageOrderAttr::where('user_id', $user->id)
                    ->where('order_id', $order->id)
                    ->where('attr_title', 'like', "%{$param['attr']}%")
                    ->first();

                if ($package_attr) {
                    $store = Store::where([['title', 'like', "%{$param['store']}%"]])->first();

                    $package_attr->used_number = $param['count'];
                    $package_attr->usable_number = $package_attr->usable_number - $param['count'];

                    $package_attr->save();

                    CarPackageUsedLog::create([
                        'user_id' => $order->user_id,
                        'attr_id' => $package_attr->id,
                        'order_id' => $order->id,
                        'package_id' => $order->package_id,
                        'package_title' => $order->package_title,
                        'package_pic' => $order->package_pic,
                        'attr_title' => $package_attr->attr_title,
                        'used_at' => $param['created_at'],
                        'store_id' => $store ? $store->id : $order->store_id,
                        'status' => DEFAULT_STATUS_SUCCESS,
                        'operator_user_id' => '',
                        'operator_at' => $param['created_at'],
                    ]);
                }
            }
        }
    }
}